package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.ZsyyCommunity;
import com.ruoyi.system.mapper.ZsyyCommunityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyInformationAddBo;
import com.ruoyi.system.bo.ZsyyInformationQueryBo;
import com.ruoyi.system.bo.ZsyyInformationEditBo;
import com.ruoyi.system.domain.ZsyyInformation;
import com.ruoyi.system.mapper.ZsyyInformationMapper;
import com.ruoyi.system.vo.ZsyyInformationVo;
import com.ruoyi.system.service.IZsyyInformationService;
import com.ruoyi.system.mapper.ZsyyInformationMapper;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 小区公告管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyInformationServiceImpl extends ServicePlusImpl<ZsyyInformationMapper, ZsyyInformation> implements IZsyyInformationService {
    @Autowired
    private ZsyyInformationMapper iZsyyInformationMapper;
    @Autowired
    private ZsyyCommunityMapper communityMapper;

    @Override
    public ZsyyInformationVo queryById(Long id){
        return getVoById(id, ZsyyInformationVo.class);
    }

    @Override
    public TableDataInfo<ZsyyInformationVo> queryPageList(ZsyyInformationQueryBo bo) {
        PagePlus<ZsyyInformation, ZsyyInformationVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyInformationVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyInformationVo> queryList(ZsyyInformationQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyInformationVo.class);
    }

    private LambdaQueryWrapper<ZsyyInformation> buildQueryWrapper(ZsyyInformationQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyInformation> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getCommunityName()), ZsyyInformation::getCommunityName, bo.getCommunityName());
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), ZsyyInformation::getTitle, bo.getTitle());
        lqw.eq(bo.getStatus() != null, ZsyyInformation::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyInformationAddBo bo) {
    	if (!CommonUtil.isEmpty(bo.getCommunityId()) && bo.getCommunityId() != 0){
			ZsyyCommunity community = communityMapper.selectById(bo.getCommunityId());
			bo.setCommunityName(community.getName());
		}else {
			bo.setCommunityName("公共内容");
		}
		bo.setCreatetime(new Date());
        ZsyyInformation add = BeanUtil.toBean(bo, ZsyyInformation.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyInformationEditBo bo) {
		if (!CommonUtil.isEmpty(bo.getCommunityId()) && bo.getCommunityId() != 0){
			ZsyyCommunity community = communityMapper.selectById(bo.getCommunityId());
			bo.setCommunityName(community.getName());
		}else{
			bo.setCommunityName("公共内容");
		}
		bo.setUpdatetime(new Date());
        ZsyyInformation update = BeanUtil.toBean(bo, ZsyyInformation.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyInformation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
